package com.college.controllers;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.college.domain.Roles;
import com.college.domain.Student;
import com.college.domain.StudentDTO;
import com.college.domain.StudentForm;
import com.college.interfaces.IConfigurationDAO;
import com.college.interfaces.IStudentDAO;
import com.college.interfaces.IStudentService;

@Controller
public class StudentController {

	@Autowired
	private IStudentService studentService;
	
	@Autowired
	private IConfigurationDAO configDAO;
	
	@Autowired
	private IStudentDAO studentDAO;
	
	
		@InitBinder
	    public void initBinder(WebDataBinder dataBinder) {
	       // dataBinder.setDisallowedFields("id");
	        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	        dateFormat.setLenient(false);
	        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	    }
	
	
	@RequestMapping(value="/addStudent", method=RequestMethod.GET)
	public ModelAndView getStudentForm(ModelMap model)
	{	List<StudentDTO> studentList = studentService.getStudentAttributesList();
		//List<Integer> userIdForDropDown = userDAO.getAllUserIdForDropDown();
		//model.addAttribute("userIdDropDown",userIdForDropDown);
		
		List<String> deptList = configDAO.getDeptListForDropDown();
		model.addAttribute("deptlist",deptList);
		List<String> batchList = configDAO.getAllBatchForDropDrown();
		model.addAttribute("batchList", batchList);
		ModelAndView mav = new ModelAndView("add_student");
		Student stu = new Student();
		mav.getModelMap().put("add_student", stu);
		StudentForm studentForm = new StudentForm();
		studentForm.setStudentList(studentList);
		model.addAttribute("studentForm",studentForm);
		return mav;
	}
	@RequestMapping(value="/addStudent", method=RequestMethod.POST)
	public String saveStudent(@ModelAttribute("add_student") StudentForm studenfForm, BindingResult result, SessionStatus status, ModelMap model) throws ParseException{
		/*if(result.hasErrors()){
			return "add_student";
		}*/
		List<StudentDTO> newList = (List<StudentDTO>) studenfForm.getStudentList();
		List<Student> newList1 = new ArrayList<Student>();
		for(StudentDTO stud:studenfForm.getStudentList()){
			Student student = new Student();
			student.setBatchId(stud.getBatchId());
			student.setBatchName(stud.getBatchName());
			student.setDeptId(stud.getDeptId());
			student.setDeptName(stud.getDeptName());
			student.setDob(stud.getDob());
			student.setDoj(stud.getDoj());
			student.setFirstName(stud.getFirstName());
			student.setLastName(stud.getLastName());
			student.setRegNo(stud.getRegNo());
			student.setRoleId(stud.getRoleId());
			student.setRoleName(stud.getRoleName());
			student.setStatus(stud.getStatus());
			student.setSex(stud.getSex());
			student.setStudentId(stud.getStudentId());
			newList1.add(student);
		}
		Integer saveStatus = studentDAO.saveStudentInfo(newList1);
		//Integer res = roleDAO.saveRole(role);
		if(saveStatus!=null){
			status.setComplete();
			model.addAttribute("savedMsg", "Student record saved Successfully.");
			
		}
		return "redirect:addStudent";
		
	}
	
	@RequestMapping(value="/addRows", method=RequestMethod.GET)
	public ModelAndView getNewRows(ModelMap model){
		List<StudentDTO> studentList = studentService.addRows();
		
		List<String> deptList = configDAO.getDeptListForDropDown();
		model.addAttribute("deptlist",deptList);
		List<String> batchList = configDAO.getAllBatchForDropDrown();
		model.addAttribute("batchList", batchList);
		ModelAndView mav = new ModelAndView("add_student");
		Student stu = new Student();
		mav.getModelMap().put("add_student", stu);
		StudentForm studentForm = new StudentForm();
		studentForm.setStudentList(studentList);
		model.addAttribute("studentForm",studentForm);
		return mav;
	}
	
	@RequestMapping(value = "/add_rows", 
			method = RequestMethod.GET, 
			headers="Accept=*/*")
			public @ResponseBody List<StudentDTO> getStudentAttributesList() {
			List<StudentDTO> rowList = studentService.getStudentAttributesList();

			return rowList;
	}
}
